package site.jlopen.secret.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.lang.TypeReference;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.model.serving.SecretServingCreateReq;
import site.jlopen.secret.model.serving.SecretServingQueryResp;
import site.jlopen.secret.service.intf.SecretServingManage;

public class SecretServingImpl implements SecretServingManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretServingImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<String> servingCreate(SecretServingCreateReq form) {
		logger.info("创建联合请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.SERVING_CREATE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> servingUpdae(SecretServingCreateReq form) {
		logger.info("修改联合请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.SERVING_UPDATE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> servingDelete(String servingId) {
		logger.info("删除联合请求：{}", servingId);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("servingId", servingId)
				.appId(clientProperties.getAppId());
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.SERVING_REMOVE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> servingStatus(String servingId) {
		logger.info("刷新联合请求：{}", servingId);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("servingId", servingId)
				.appId(clientProperties.getAppId());
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.SERVING_STATUS_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<SecretServingQueryResp> servingQuery(String servingId) {
		logger.info("查询联合请求：{}", servingId);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("servingId", servingId)
				.appId(clientProperties.getAppId());
		SecretClientHttp<SecretServingQueryResp> clientRequest = new SecretClientHttp<SecretServingQueryResp>(clientProperties);
		SecretClientResponse<SecretServingQueryResp>  res = clientRequest.execute(SecretReqUrl.SERVING_INFO_URL, form, new TypeReference<SecretClientResponse<SecretServingQueryResp>>() {});
		return res;
	}

}
